package com.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.util.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by marlon on 2016-04-20.
 */
public class CommonToolBar extends RelativeLayout {
    private Context context;
    @Bind(R.id.rl_commonToolBar)
    RelativeLayout rl;
    @Bind(R.id.iv_back_commonToolBar)
    ImageView iv_back;
    @Bind(R.id.tv_title_commonToolBar)
    TextView tv_title;
    @Bind(R.id.iv_menu_commonToolBar)
    ImageView iv_menu;

    public CommonToolBar(Context context) {
        super(context);
    }

    public CommonToolBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initToolBar();
        initStyle(attrs);
    }

    //初始化自定义属性
    private void initStyle(AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CommonToolBar);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CommonToolBar_barBackGround:
                    rl.setBackgroundColor(a.getColor(attr, Color.BLUE));
                    break;
                case R.styleable.CommonToolBar_isShowBackImage:
                    if (!a.getBoolean(attr, false)) { //隐藏回退按钮
                        iv_back.setVisibility(View.GONE);
                    }
                    break;
                case R.styleable.CommonToolBar_backImage:
                    iv_back.setImageResource(a.getResourceId(attr, -1));
                    break;
                case R.styleable.CommonToolBar_middleTitleText:
                    tv_title.setText(a.getString(attr));
                    break;
                case R.styleable.CommonToolBar_middleTitleTextColor:
                    tv_title.setTextColor(a.getColor(attr, Color.WHITE));
                    break;
                case R.styleable.CommonToolBar_middleTitleTextSize:
                    tv_title.setTextSize(a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                            12, getResources().getDisplayMetrics())));
                    break;
                case R.styleable.CommonToolBar_isShowMenuImage:
                    if (!a.getBoolean(attr, false)) {
                        iv_menu.setVisibility(View.GONE);
                    }
                    break;
                case R.styleable.CommonToolBar_menuImage:
                    if (!a.getBoolean(attr, false)) {
                        iv_menu.setImageResource(a.getResourceId(attr, -1));
                    }
                    break;

            }
        }
        a.recycle();
    }

    //设置中部标题文本
    public void setMiddleTitleText(String text) {
        tv_title.setText(text);
    }


    //初始化toolbar
    private void initToolBar() {
        View view = LayoutInflater.from(context).inflate(R.layout.view_common_toolbar, this);
        ButterKnife.bind(view);
    }

    //回退按钮点击事件
    @OnClick(R.id.iv_back_commonToolBar)
    public void onBackClick() {
        ((Activity) context).finish();
    }
}
